using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using Game.Config;


//角色数值配置

[CreateAssetMenu(menuName = "配置/创建状态配置")]
public class StateScripttableObject : ScriptableObject,ISerializationCallbackReceiver
{

    //所有实时编辑的工具
    [SerializeField]
    [ListDrawerSettings(ShowIndexLabels = true,ShowPaging  = false,ListElementLabelName ="info" )]
    public List<StateEntity> states = new List<StateEntity>();

    public void OnAfterDeserialize()
    {
        
    }

    public void OnBeforeSerialize()
    {
#if UNITY_EDITOR
        if(states.Count == 0)
        {
            Dictionary<int, PlayerStateEntity> dct = PlayerStateData.all;
            foreach(var item in dct)
            {
                var info = item.Value;
                StateEntity entity = new StateEntity()
                {
                    id = info.id,
                    info = $"{info.id}{info.info}",                  
                };
                states.Add(entity);
            }
        }
        else
        {
            Dictionary<int, PlayerStateEntity> dct = PlayerStateData.all;
            if(dct.Count != states.Count)
            {
                foreach(var item in dct)
                {
                    var info = item.Value;
                    bool add = true;
                    for(int i = 0; i < states.Count; i++)
                    {
                        if (states[i].id == info.id)
                        {
                            add = false;
                            continue;
                        }
                    }

                    //增加
                    if(add == true)
                    {
                        StateEntity entity = new StateEntity()
                        {
                            id = info.id,
                            info = $"{info.id}{info.info}",
                        };
                        states.Add(entity);
                    }
                }

                List<StateEntity>remove = new List<StateEntity>();
                foreach(var item  in states)
                {
                    if (dct.ContainsKey(item.id) ==false)
                    {
                        remove.Add(item);
                    }
                }

                foreach(var item in remove)
                {
                    states.Remove(item);
                }
            }
        }
#endif
    }
}

[Serializable]
public class StateEntity 
{
    public int id;
    public string info;

    [Header("是否忽略单位碰撞")]
    public bool ignor_collision;

    [Header("物理位移配置")]
    public List<PhysicsConfig> physicsConfig;

    [Header("子物体显示/隐藏控制 ")]
    public List<Obj_State> obj_States;

    [Header("顿帧配置")]
    public List<HitlagConfig> hitlagConfigs;

    [Header("径向模糊配置")]
    public List<RadialBlurConfig> radialBlurConfigs;

    [Header("命中检测配置")]
    public List<HitConfig> hitConfigs;

    [Header("特效配置")]
    public List<EffectConfig> effectConfigs;

}

[Serializable]
public class PhysicsConfig
{

    [Header("触发点")]
    public float trigger;//动作触发时间点

    [Header("结束点")]
    public float time; //动作结束时间点

    [Header("位移长度/力")]
    public Vector3 force;

    [Header("曲线配置")]
    public AnimationCurve cure = AnimationCurve.Constant(0,1,1);

    [Header("是否忽略重力")]
    public bool ignore_gravity;

    [Header("检测到单位后停下 距离单位多少米停下")]
    public float stop_dst;
}

[System.Serializable]
public class Obj_State
{
    [Header("注释说明")]
    public string info;

    [Header("触发点/开始点")]
    public float trigger;


    [Header("需要操作的物体")]
    public string[] obj_id;


    [Header("打勾激活/反之隐藏")]
    public bool act;

    [Header("状态提前结束 是否强制提前结束")]
    public bool force;

    [Header("是否循环执行(循环动作)")]
    public bool loop;
}

[System.Serializable]
public class HitlagConfig
{

    [Header("触发点/开始点")]
    public float trigger;

    [Header("顿帧帧数")]
    public int frame;

    [Header("触发方式")]
    public int triggerType; //触发方式： 0直接触发 1命中触发

    [Header("命中触发点2")] //和触发点1一起用 在 t1--t2内命中敌人 才顿
    public float trigger2;

    [Header("是插值")]
    public bool lerp ;
}


[System.Serializable]
public class RadialBlurConfig
{

    [Header("触发点")]
    public float trigger;
    [Header("激活或者关闭")]
    public bool active;
    [Header("平滑时间")]
    public float lerp;
}


[System.Serializable]
public class HitConfig
{

    [Header("触发点")]
    public float trigger;

    [Header("结束点")]
    public float end;

    [Header("类型：0射线 1盒子")]
    public int type;


    [Header("射线:起点 配置子物体路径")]
    public string begin;


    [Header("射线长度")]
    public float length;

    [Header("命中特效")]
    public string hitObj;


    [Space(20)]
    [Header("盒子中心 相对坐标")]
    public Vector3 box_center;

    [Header("盒子大小")]
    public Vector3 box_size;

    //每一帧检测 感兴趣的范围 :距离 角度



}

[Serializable]
public class EffectConfig
{
    [Header("资源路径")]
    public string res_path;
    [Header("触发点")]
    public float trigger;
    [Header("当前使用的道具ID")]
    public int use_prop_id;//0则没有该条件的判断
    [Header("生成方式:0单发 1多发散射 2矩形队列 3范围内随机")]
    public int create_type;

    [Space(15)]
    [Header("出生-参考位置:0自身 1目标")]
    public int spawn_point_type;
    [Header("出生-挂点名称")]
    public string spawn_hang_point;
    [Header("出生-朝向:0挂点方向 1朝向目标 2自身前方 3目标前方")]
    public int rotate_type;
    [Header("出生-位置偏差")]
    public Vector3 position_offset;


    [Space(15)]
    [Header("扇形-生成数量(左右多少)")]
    public int fan_count;
    [Header("扇形-间隔角度")]
    public int fan_angle_difference;

    [Space(15)]
    [Header("矩形-生成多少行")]
    public int rect_rows;
    [Header("矩形-生成多少列")]
    public int rect_columns;
    [Header("矩形-每行间隔")]
    public float rect_rows_spacing;
    [Header("矩形-每列间隔")]
    public float rect_columns_spacing;

    [Space(15)]
    [Header("随机范围-半径(最小)")]
    public float random_radius;
    [Header("随机范围-半径(最大)")]
    public float random_radius_max;
    [Header("随机范围-数量")]
    public int random_count;
    [Header("随机范围-数量(最大)")]
    public int random_count_max;

    [Header("随机范围-角度")]
    public float random_angle;
    [Header("随机范围-角度(最大)")]
    public float random_angle_max;

    [Space(30)]
    [Header("移动的方式:0按方向移动 1追踪移动 2围绕旋转 3贝塞尔曲线移动 4跟随移动")]
    public int move_type;
    //[Header("按方向-移动")]
    //public DirectMoveConfg directMoveConfg;

    //[Header("追踪目标-移动")]
    //public TrackMoveConfig trackMoveConfig;
    //[Header("围绕旋转-移动")]
    //public AroundMoveConfig aroundMoveConfig;
    //[Header("曲线-移动")]
    //public BezierCurveMoveConfig bezierCurveMoveConfig;

    //[Header("跟随-移动")]
    //public FollowTweenConfig followTween;

    [Space(30)]
    [Header("命中时的特效")]
    public string hit_effect;
    [Header("命中时特效数量:0无限制 1只创建一次")]
    public int hit_effect_count;
    [Header("命中时的音效")]
    public string hit_audio;

    [Header("同一个单位允许被命中几次")]
    public int unit_hit_count;


    [Space(30)]
    [Header("销毁-存活时间")]
    public float destroy_durtaion;

    [Header("销毁-命中多少个单位")]
    public int destroy_hit_count;
}